<template>
  <div>
    <el-dialog :title="bm" :visible="showDialog" @close="close">
      <el-form ref="Form" label-width="120px" :model="form" :rules="rules">
        <el-form-item prop="name" label="部门名称">
          <el-input v-model="form.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="form.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4" style="width: 80%" />
        </el-form-item>
        <el-form-item>
          <!-- 按钮 -->
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="submit">确定</el-button>
              <el-button size="mini" @click="closeFn">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getManagerList, addDept, editDept } from '@/api/department'
import { getDepDetail } from '@/api/department'
export default {
  props: {
    showDialog: Boolean,
    newdeps: {
      type: Array,
      required: true
    },
    currentNodeId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      form: {
        code: '',
        name: '',
        managerId: '',
        introduce: '',
        currentNodeId: ''
      },
      list: [],
      rules: {
        name: [{
          required: true, message: '输入部门名称', trigger: 'blur'
        },
        { min: 2, max: 10, message: '名称在2到10位', trigger: 'blur' },
        { validator: (rule, value, callback) => {
          let deps = this.newdeps
          // 声明一个变量接收传过去的所以数据
          if (this.form.id) {
            // 判断如果是表单里面的数据
            // 那么就过滤掉这个数据
            deps = deps.filter(item => item.id !== this.form.id)
          }
          // 过滤后在拿过来进行校验
          // 自定义判断如果有一个重复则返回true也就是提示重复，反之则执行
          const res = deps.some(item => item.name === value)
          res ? callback(new Error('部门名称重复')) : callback()
        },
        trigger: 'blur'
        }
        ],
        code: [{
          required: true, message: '输入部门编号', trigger: 'blur'
        },
        { min: 2, max: 10, message: '编号在2到10位', trigger: 'blur' },
        { validator: (rule, value, callback) => {
          const res = this.newdeps.some(item => {
            // 声明一个常量接收遍历后的数据
            // 遍历这个数据 如果编码等于当前校验的数据，后面的恒等于true
            return item.code === value && item.id !== this.form.id
          })
          res ? callback(new Error('部门编号重复')) : callback()
        },
        trigger: 'blur'
        }
        ],
        introduce: [{
          required: true, message: '输入部门介绍', trigger: 'blur'
        },
        { min: 2, max: 100, message: '介绍在2到100位', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    bm() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    getManagerList() {
      getManagerList().then(res => {
        console.log(res)
        this.list = res
      })
    },
    closeFn() {
      this.form = {
        code: '',
        name: '',
        managerId: '',
        introduce: '',
        currentNodeId: '',
        pid: ''
      }
      this.$refs.Form.resetFields()
      this.$emit('update:showDialog', false)
    },
    close() {
      this.$emit('update:showDialog', false)
    },
    submit() {
    // 首先判断是否通过表单校验
      this.$refs.Form.validate(async pass => {
        if (pass) {
          this.form.id ? await editDept({ ...this.form })
          // 如果通过校验就调用接口并传参过去
            : await addDept({
              ...this.form, pid: this.currentNodeId
            })
          // 添加成功后向父组件发起通知更新
          // 并提示添加成功,在调用关闭
          this.$emit('updatwDeps')
          this.$message.success(`${this.form.id ? '编辑' : '新增'}成功`)
          this.closeFn()
        }
      })
    },
    async getDepDetail() {
      // console.log(this.currentNodeId === true)
      this.form = await getDepDetail(this.currentNodeId)
    }
  }

}
</script>

<style>

</style>
